﻿using AnQinFrameWorkAPI.DataBase;
using AnQinFrameWorkAPI.Entity.SystemCore.DTO.SystemConfig;
using AnQinFrameWorkAPI.Entity.SystemCore.Entity;
using AnQinFrameWorkAPI.Entity.SystemCore.View.SystemConfig;
using AutoMapper;
using AutoMapper.QueryableExtensions;
using FrameWork.DataHelper;
using FrameWork.Entity;
using FrameWork.Request;
using Microsoft.EntityFrameworkCore;

namespace AnQinFrameWorkAPI.Service.SystemCore;

/// <summary>
///     全局配置业务层
/// </summary>
public class SystemConfigService
{
    //数据库
    private readonly DatabaseContext _context;

    //自动映射
    private readonly IMapper _mapper;

    //构造注入
    public SystemConfigService(DatabaseContext context, IMapper mapper)
    {
        _context = context;
        _mapper = mapper;
    }

    #region 获取全局配置

    public async Task<RequestResponseModel> GetSystemConfigList(SystemConfigSearchDTO systemConfigSearchDTO)
    {
        //筛选
        var query = _context.SystemConfig
                .ProjectTo<SystemConfigView>(_mapper.ConfigurationProvider)
                .WhereIF(
                    a => a.Code.Contains(systemConfigSearchDTO.Keyword) ||
                         a.Content.Contains(systemConfigSearchDTO.Keyword),
                    !string.IsNullOrEmpty(systemConfigSearchDTO.Keyword))
                .WhereIF(a => a.CreateTime > systemConfigSearchDTO.StartTime, systemConfigSearchDTO.StartTime != null)
                .WhereIF(a => a.CreateTime < systemConfigSearchDTO.EndTime, systemConfigSearchDTO.EndTime != null)
                .AsNoTracking()
            ;

        //返回列表
        var SystemConfigList = await query
            .OrderBy(a => a.CreateTime)
            .Page(systemConfigSearchDTO.PageIndex, systemConfigSearchDTO.PageSize)
            .ToListAsync();

        //总数据量
        var pageCount = await query.CountAsync();

        //返回数据
        var pageResult = new PageResult<SystemConfigView>(SystemConfigList, pageCount);
        return RequestResponse.OK(pageResult, "查询成功");
    }

    #endregion

    #region 新增全局配置

    public async Task<RequestResponseModel> AddSystemConfig(SystemConfigAddDTO systemConfigAddDTO)
    {
        //生成新数据
        var systemConfig = _mapper.Map<SystemConfigModel>(systemConfigAddDTO);

        systemConfig.Id = Guid.NewGuid();
        systemConfig.CreateTime = DateTime.Now;

        //新增数据
        _context.SystemConfig.Add(systemConfig);
        await _context.SaveChangesAsync();

        return RequestResponse.OK(systemConfig.Id, "新增成功");
    }

    #endregion

    #region 更新全局配置

    public async Task<RequestResponseModel> PutSystemConfig(SystemConfigUpdateDTO systemConfigUpdateDTO)
    {
        //校验数据
        var query = await Valid.ValidId(_context.SystemConfig, systemConfigUpdateDTO.Id);
        if (query == null) return RequestResponse.ErrorRequest("全局配置Id无效");
        ;

        //编辑数据
        var systemConfig = _mapper.Map(systemConfigUpdateDTO, query);

        //更新表
        _context.SystemConfig.Update(systemConfig);
        await _context.SaveChangesAsync();

        return RequestResponse.OK(systemConfigUpdateDTO.Id);
    }

    #endregion

    #region 删除全局配置

    public async Task<RequestResponseModel> DeleteSystemConfig(SingleIdDTO deleteDTO)
    {
        //校验数据
        var query = await Valid.ValidId(_context.SystemConfig, deleteDTO.Id);
        if (query == null) return RequestResponse.ErrorRequest("全局配置Id无效");
        ;

        //删除操作
        _context.SystemConfig.Remove(query);

        //保存库
        await _context.SaveChangesAsync();
        return RequestResponse.OK(deleteDTO.Id, "删除成功");
    }

    #endregion
}